package com.dwin.dingnuo.views;

import android.app.Dialog;
import android.content.Context;


import com.dwin.dingnuo.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 网络请求对话框
 */
public class DeviceWaitingDialog extends Dialog {

    private static DeviceWaitingDialog mLoadingProgress;
    private static List<DeviceWaitingDialog> dialogs = new ArrayList<>();

    public DeviceWaitingDialog(@NonNull Context context) {
        super(context);
    }

    public DeviceWaitingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected DeviceWaitingDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static void showprogress(Context context, boolean iscanCancel) {
        mLoadingProgress = new DeviceWaitingDialog(context, R.style.DialogTheme);
        //自定义style文件主要让背景变成透明并去掉标题部分<!-- 自定义loading dialog -->
        mLoadingProgress.setCanceledOnTouchOutside(false);
        mLoadingProgress.setTitle("");
        mLoadingProgress.setContentView(R.layout.loading_layout);
        mLoadingProgress.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        // 按返回键响应是否取消等待框的显示
        mLoadingProgress.setCancelable(iscanCancel);
        if (dialogs.isEmpty()){
            mLoadingProgress.show();
            dialogs.add(mLoadingProgress);
        }
    }

    public static void dismissprogress() {
        for (DeviceWaitingDialog dialog : dialogs) {
            if (dialog != null) {
                dialog.dismiss();
                dialog = null;
            }
        }
        dialogs.clear();
//        if (mLoadingProgress != null) {
//            mLoadingProgress.dismiss();
//        }
    }

}
